<?php

class setupAdmin extends Controller{
    
    private $steps = array(
        'Instalação do banco de dados' => 'dbinstall', 
        'Criar uma conta de Webmaster' => 'webmaster', 
        'Configurações básicas'        => 'configurerecursos',
        'Instalação dos plugins'       => ''
    );
    
    public function AfterLoad() {
        parent::AfterLoad();
        $this->LoadModel("plugins/setup", 'sobj');
        $this->url = URL . "admin/install.php?url=index";
        $var = "<ul>";
        foreach($this->steps as $name => $action){
            if(CURRENT_ACTION == $action) $name = "<span style='color:red;'>$name</span>";
            $var .= "<li>$name</li>";
            if($action == 'configurerecursos' && CURRENT_ACTION == 'configurerecursos'){
                $out = $this->gensession();
                $page = isset($_GET['page'])?$_GET['page']:'0';
                $i = 0;
                $var .= "<ul>";
                foreach($out as $o){
                    if(@$_GET['page'] == $i) $o = "<span style='color:red;'>$o</span>";
                    $var .= "<li>$o</li>";
                    $i++;
                }
                $var .= "</ul>";
            }
        }
        $var .= "</ul>";
        EventTube::addEvent("menu-lateral", $var);
        try{
            $this->LoadModel('usuario/login', 'uobj');
        }  catch (DBException $e){
            $this->registerVar('alert', $e->getMessage());
        };
    }

    public function dbinstall(){
        
        if($this->sobj->hasDbConnection()) SRedirect($this->url);
        if(!$this->sobj->dbInstall($_POST)){
            $this->setVars($this->sobj->getMessages());
            $this->registerVar("dados", $this->sobj->getDados());
            $this->genTags("Configurar o banco de dados");
            $this->display('admin/install/forminstall');
        }
        else SRedirect($this->url);
    }
    
    public function tableinstall(){
        
        if($this->sobj->CheckBasicTables()) { SRedirect($this->url);}
        $this->setVars($this->sobj->getMessages());
        $this->display("");
    }
    
    public function webmaster(){
        
        $this->uobj->Logout();
        if(!$this->sobj->hasWebmaster()){
            $this->genTags("Criar um novo Webmaster");
            $bool = $this->sobj->setWebmaster($_POST);
            $this->setVars($this->sobj->getMessages());
            $this->registerVar('status', $bool?'1':"0");
            if(isset($_REQUEST['ajax']) && $bool){
                $this->registerVar('redirect', $this->url);
            }
            
            $this->registerVar("dados", $this->sobj->getWebmasterData());
            $this->registerVar('titulo', "Dados do Webmaster");
            $this->display('admin/auto/formulario');
            
        }else SRedirect($this->url);
            
    }
    
    private function gensession($block = false){
        $this->LoadModel('admin/configuracao', 'model');
        //if(!isset($_SESSION['temp'])){
            $out = array();
            $config = $this->model->selecionar();
            foreach($config as $conf => $arr){
                foreach($arr as $name => $var){
                    if(is_array($var)){
                        foreach($var as $nm => $ar){
                            $out[$ar] = $nm;
                        }
                    }else $out[$name] = $var;
                }
            }
            
            if(!$block){
                $this->model->setConfigType("Config");
                $new = $this->gensession(true);
                foreach($new as $name => $var){
                    $out[$name] = $var;
                }
                $_SESSION['temp'] = serialize($out);
            }
            
        //}else $out = unserialize ($_SESSION['temp']);
        return $out;
    }
    
    public function configurerecursos(){
        
        $this->LoadModel('usuario/login', 'uobj');
        $this->uobj->needWebmasterLogin();
        if(!isset($_GET['page'])){ SRedirect(URL ."admin/install.php?url=configurerecursos&page=0");}
        $this->LoadModel('admin/configuracao', 'model');
        $out = $this->gensession();
        
        $i = 0;
        foreach($out as $url => $name){
            if($i == $_GET['page']){
                $this->vars[] = $url;
                break;
            }
            $i++;
        }
        $i++;
        if($_GET['page'] <= count($out)) $this->configure($i);
        else SRedirect(URL ."admin/index.php?url=admin/install");
    }
    
    private function configure($i){
        $this->LoadModel('admin/configuracao', 'model');
        $conffile = implode("/",$this->vars);
        if(!$this->model->hasConfigurationFile($conffile)){
            $this->model->setConfigType("Config");
            if(!$this->model->hasConfigurationFile($conffile)){
                SRedirect(URL ."admin/install.php?url=configurerecursos&page=$i");
            }
        }
        
        if(!empty ($_POST)){
            if($this->model->editar($conffile, $_POST)){           
                SRedirect(URL ."admin/install.php?url=configurerecursos&page=$i");
            }else $this->setVars($this->model->getMessages());
        }
        
        $this->genTags("Configurar $conffile");
        $this->registerVar("cfile", $conffile);
        $this->registerVar("dados", $this->model->getConf($conffile));
        $this->setVars($this->model->getMessages());
        $this->display('plugins/setup/configure');
    }
    
    public function index(){
        
        /*$this->setVars($this->sobj->getMessages());
        $erro  = $this->sobj->getErrorMessage(); if($erro != "") {$this->display (""); return; }
        $alert = $this->sobj->getAlertMessage(); if($alert != "") {$this->display (""); return; }
        */
        if(!$this->sobj->hasDbConnection()) SRedirect(URL ."admin/install.php?url=dbinstall");
        if(!$this->sobj->CheckBasicTables()) {$this->tableinstall(); return;}
        if(!$this->sobj->hasWebmaster()) SRedirect(URL ."admin/install.php?url=webmaster");
        SRedirect(URL ."admin/install.php?url=configurerecursos");
    }
    
}
?>
